@Typed package org.mbte.groovypp.examples.wordcount

import java.util.concurrent.*
import groovy.util.concurrent.*

for (i in 0..<10) {

    def t1 = System.currentTimeMillis()
    def counts = new AtomicIntegerMap ()
    def pool = Executors.newFixedThreadPool(10)

    new File("./20_newsgroups").recurseFileIterator().filter{ file ->
      !file.directory && !file.path.contains(".svn")
    }.mapConcurrently(pool, false, 2) { file ->
        file.charSequence.eachMatch(/\w+/) { String w -> w = w.toLowerCase();
            counts[w].incrementAndGet ()
        }
    }.each{}

    pool.execute {
        new File("counts-descreasing-groovy").withWriter { Writer out ->
          counts.asList().sort { a, b -> b.value - a.value }.each { e -> out << "$e.key\t${e}\n" }
        }
    }

    pool.execute {
        new File("counts-alphabetical-groovy").withWriter { Writer out ->
          counts.asList().sort { a, b -> b.key <=> a.key }.each { e -> out << "$e.key\t${e}\n" }
        }
    }
    pool.shutdown()
    pool.awaitTermination(30,TimeUnit.SECONDS)

    println "Finished in ${System.currentTimeMillis() - t1} millis"
}
